package soccerscope.training.conditions;

import robocup.component.geometry.Vektor;
import soccerscope.model.ClientScene;

public class BallPossessionCondition
    extends GameCondition {

  boolean leftTeam = false;

  public BallPossessionCondition() {

    super();
  }

  public void setLeftTeam(boolean leftTeam) {

    this.leftTeam = leftTeam;
  }

  public boolean isLeftTeam() {

    return this.leftTeam;
  }

  /**
   * This method checks wheather the specified Team is in Ball-Possession. For
   * performance and simplicity reasons, an estimation is used here. If the Team
   * is in direct ball-possession, i.e., a player can kick the ball without the
   * ability of another teams player can do so, this condition is true.
   */
  @Override
  public boolean isTrue(ClientScene scene) {

    Vektor ball = new Vektor(Vektor.XY, scene.ball.pos.y, scene.ball.pos.x);

    double dist = 1;
    int pNum = -1;

    for (int i = 0; i < scene.player.length; i++) {
      Vektor player = new Vektor(Vektor.XY, scene.player[i].pos.y,
          scene.player[i].pos.x);
      double pDist = player.getDistance(ball);
      if (pDist < dist) {
        pNum = i;
        if (this.leftTeam && pNum >= 11) {
          return false;
        }
        if (!this.leftTeam && pNum < 11) {
          return false;
        }
      }
    }

    if (pNum == -1) {
      return false;
    }
    // System.out.println("" + WorldModel.getInstance().
    // getSceneSet().lastScene().time +
    // " Ball-Possession-Condition");
    return true;

  }

  /**
   * Nothing to reset in the moment
   */
  @Override
  public void resetCondition() {

    return;
  }

  public int getStatIndex() {

    return 2;
  }

}
